package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

@Component
public class SpringRabbitListener {
   /* @RabbitListener(queues = "simple.queue")
    public  void  listenSimpleQueue(String msg){
        System.out.println("接收到："+msg);
    }*/
  /* @RabbitListener(queues = "simple.queue")
   public  void  listenSimpleQueue1(String msg) throws InterruptedException {
       System.out.println("1接收到："+msg+ LocalDateTime.now());
       Thread.sleep(20);
   }
    @RabbitListener(queues = "simple.queue")
    public  void  listenSimpleQueue2(String msg) throws InterruptedException {
        System.err.println("2接收到："+msg+ LocalDateTime.now());
        Thread.sleep(200);
    }*/

    @RabbitListener(queues = "fanout.queue1")
    public  void  listenFanoutQueue1(String msg) throws InterruptedException {
        System.err.println("2接收到Fanout1："+msg+ LocalDateTime.now());

    }
    @RabbitListener(queues = "fanout.queue2")
    public  void  listenFanoutQueue2(String msg) throws InterruptedException {
        System.err.println("2接收到Fanout2："+msg+ LocalDateTime.now());

    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange =@Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public  void  listenDirectQueue1(String msg) throws InterruptedException {
        System.out.println("1接收到Direct1："+msg+ LocalDateTime.now());
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange =@Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public  void  listenDirectQueue2(String msg) throws InterruptedException {
        System.out.println("2接收到Direct2："+msg+ LocalDateTime.now());
    }
    //topic
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange =@Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key ="china.#"
    ))
    public  void  listenTopicQueue1(String msg) throws InterruptedException {
        System.out.println("21接收到Topic1："+msg+ LocalDateTime.now());
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange =@Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public  void  listenTopicQueue2(String msg) throws InterruptedException {
        System.out.println("2接收到topic2："+msg+ LocalDateTime.now());
    }
    @RabbitListener(queues = "object.queue")
    public  void listenObjectQueue(Map<String,Object> msg){
        System.out.println("接受到map:"+msg);
    }
}
